package com.wushijia.algorithm;

import java.util.Stack;

/**
 * 入栈/弹出时间复杂度O(1)，弹出最小元素
 *
 * @author Administrator
 * @date 2021-06-10 14:24
 */
public class MyStack1 {

  private Stack<Integer> stackData;
  private Stack<Integer> stackMin;

  public MyStack1() {
    this.stackData = new Stack<>();
    this.stackMin = new Stack<>();
  }

  public void push(int newNum) {
    if (stackMin.isEmpty()) {
      stackMin.push(newNum);
    } else if (this.getMin() >= newNum) {
      stackMin.push(newNum);
    }
    stackData.push(newNum);
  }

  public int pop() {
    if (this.stackData.empty()) {
      throw new RuntimeException("Your stack is empty");
    }
    int value = this.stackData.pop();
    if (value == this.getMin()) {
      this.stackMin.pop();
    }
    return value;
  }

  public int getMin() {
    if (stackMin.empty()) {
      throw new RuntimeException("Your stack is empty");
    }
    return stackMin.peek();
  }
}
